package top.codedance.iotp.server.net.handle.subhandle.controller;

import com.alibaba.fastjson.JSONObject;
import top.codedance.iotp.common.entity.UserEntity;
import top.codedance.iotp.common.enums.HttpStatus;
import top.codedance.iotp.server.ServerApplication;
import top.codedance.iotp.server.net.handle.util.UserService;

import java.nio.charset.StandardCharsets;

public abstract class AdapterController implements Controller {

    private JSONObject jsonObject = new JSONObject();

    public String doHandler(byte[] bodyArr) {
        try {
            String bodyString = new String(bodyArr, StandardCharsets.UTF_8);
            this.jsonObject = JSONObject.parseObject(bodyString);
            return doSubHandler(this.jsonObject);
        } catch (Exception e) {
            return response500("Programe exception");
        }
    }

    /**
     * 处理的controller需要实现
     * @param body
     * @return
     */
    public abstract String doSubHandler(JSONObject body);

    /**
     * 用户是否通过验证
     * @return
     */
    protected boolean userVerification() {
        String deviceId = jsonObject.getString("deviceId");
        String username = jsonObject.getString("username");
        String password = jsonObject.getString("password");
        if(ServerApplication.enableCache){
            return ServerApplication.userCacheVerification(deviceId, username, password);
        }else {
            UserEntity userEntity = new UserEntity();
            userEntity.setUsername(username);
            userEntity.setPassword(password);
            userEntity.setDeviceId(deviceId);
            if(ServerApplication.userVerifitionPrivateKey == null) {
                return UserService.isExits(userEntity);
            }else{
                return UserService.isExits(userEntity, ServerApplication.userVerifitionPrivateKey);
            }
        }
    }

    /**
     * 判断是否是管理员
     * @return
     */
    protected boolean adminVerification() {
        String username = jsonObject.getString("username");
        String password = jsonObject.getString("password");
        if(ServerApplication.enableCache){
            return ServerApplication.adminCacheVerification(username, password);
        }else {
            UserEntity userEntity = new UserEntity();
            userEntity.setUsername(username);
            userEntity.setPassword(password);
            if(ServerApplication.userVerifitionPrivateKey == null) {
                return UserService.isExitsOfAdmin(userEntity);
            }else{
                return UserService.isExitsOfAdmin(userEntity, ServerApplication.userVerifitionPrivateKey);
            }
        }
    }

    /**
     * 分布式模式下组件通讯验证
     * @return
     */
    protected boolean netVerification(){
        String username = jsonObject.getString("username");
        String password = jsonObject.getString("password");
        return username.equals(ServerApplication.netUsername) && password.equals(ServerApplication.netPassword);
    }

    protected String response(int statusCode, Object msg, Object data) {
        JSONObject result = new JSONObject();
        result.put("status", statusCode);
        if(HttpStatus.OK.getCode() != statusCode) {
            result.put("message", msg);
        }else{
            result.put("data", data);
        }
        return result.toJSONString();
    }

    protected String okResponse(Object data) {
        return response(HttpStatus.OK.getCode(), "", data);
    }

    protected String errResponse(int statusCode, Object msg) {
        return response(statusCode, msg, null);
    }

    protected String response500(String msg) {
        return errResponse(HttpStatus.INTERNAL_SERVER_ERROR.getCode(), msg);
    }

    protected String response403(String msg){
        return errResponse(HttpStatus.FORBIDDEN.getCode(), msg);
    }
}